package com.qiaoba.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.module.system.converter.SysPostConverter;
import com.qiaoba.module.system.entity.SysPost;
import com.qiaoba.module.system.entity.dto.SysPostDTO;
import com.qiaoba.module.system.entity.param.SysPostParam;
import com.qiaoba.module.system.mapper.SysPostMapper;
import com.qiaoba.module.system.service.SysPostService;
import com.qiaoba.module.system.service.SysUserPostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 岗位 服务层实现
 *
 * @author ailanyin
 * @date 2023-09-10 08:53:20
 */
@Service
@RequiredArgsConstructor
public class SysPostServiceImpl implements SysPostService {

    private final SysPostMapper sysPostMapper;
    private final SysUserPostService sysUserPostService;

    @Override
    public int insert(SysPostDTO dto) {
        checkPostNameUnique(dto.getPostId(), dto.getPostName());
        checkPostCodeUnique(dto.getPostId(), dto.getPostCode());
        return sysPostMapper.insert(SysPostConverter.dto2Entity(dto));
    }

    @Override
    public int updateById(SysPostDTO dto) {
        checkPostNameUnique(dto.getPostId(), dto.getPostName());
        // 业务上不允许修改code
        dto.setPostCode(null);
        return sysPostMapper.updateById(SysPostConverter.dto2Entity(dto));
    }

    @Override
    public int deleteById(String postId) {
        return deleteByIds(CollUtil.toList(postId));
    }

    @Override
    public int deleteByIds(List<String> postIds) {
        checkAllowDelete(postIds);
        return sysPostMapper.deleteBatchIds(postIds);
    }

    @Override
    public SysPost selectById(String postId, boolean allowNull) {
        SysPost sysPost = sysPostMapper.selectById(postId);
        if (!allowNull && Objects.isNull(sysPost)) {
            throw new ServiceException(StrUtil.format("查询岗位信息不存在, ID: {}", postId));
        }
        return sysPost;
    }

    @Override
    public List<SysPost> selectList(SysPostParam param) {
        return sysPostMapper.selectList(SysPostConverter.param2Wrapper(param));
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param postId   岗位ID
     * @param postName 岗位名称
     */
    private void checkPostNameUnique(String postId, String postName) {
        if (StrUtil.isNotBlank(sysPostMapper.checkPostNameUnique(postId, postName))) {
            throw new ServiceException(StrUtil.format("保存岗位{}失败, 岗位名称已存在", postName));
        }
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param postId   岗位ID
     * @param postCode 岗位编码
     */
    private void checkPostCodeUnique(String postId, String postCode) {
        if (StrUtil.isNotBlank(sysPostMapper.checkPostCodeUnique(postId, postCode))) {
            throw new ServiceException(StrUtil.format("保存岗位{}失败, 岗位编码已存在", postCode));
        }
    }

    /**
     * 检查岗位是否允许删除
     *
     * @param ids ids
     */
    private void checkAllowDelete(List<String> ids) {
        List<String> names = sysUserPostService.selectUsedPostNameByIds(ids);
        if (CollUtil.isNotEmpty(names)) {
            throw new ServiceException(StrUtil.format("删除岗位{}失败, 岗位已绑定用户", names.toString()));
        }
    }
}
